﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using DB;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            SetOptableVisible(false);
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Button btn =(Button)sender;
        EditItem(btn.CommandArgument.ToString());
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string id = btn.CommandArgument.ToString();
        DataAccess da = new DataAccess();
        da.RunSql("delete from accounts where id='" + id + "'");
        GridView1.DataBind();
        RegisterClientScriptBlock("abcde", "<script>alert('Operation sucess.')</script>");
    }

    private void EditItem(string id)
    {
        DataAccess da = new DataAccess();
        SqlDataReader dr = da.GetDataReader("select id,cardNo,userName,userPassword,balance from accounts where id='" + id + "'");
        if (dr.Read())
        {
            SetTextBoxValue("E", dr.GetValue(0).ToString(), dr.GetValue(1).ToString(), dr.GetValue(2).ToString(), dr.GetValue(3).ToString(), dr.GetValue(4).ToString());
            SetOptableVisible(true);
        }
    }

    private void SetTextBoxValue(string state,string id, string cardNo, string userName, string password, string balance)
    {
        if (state == "E")
        {
            hidID.Value = id;
            txtCardNo.Text = cardNo;
            txtUserName.Text = userName;
            txtPassword.Text = password;
            txtBalance.Text = balance;
            btnNE.CommandArgument = state;
            btnNE.Text = "Update";
        }
        else if (state == "N")
        {
            txtCardNo.Text = "";
            txtUserName.Text = "";
            txtPassword.Text = "";
            txtBalance.Text = "";
            btnNE.CommandArgument = state;
            btnNE.Text = "New";
        }
    }

    protected void btnNE_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        string state = btn.CommandArgument.ToString();
        DataAccess da = new DataAccess();
        string sqlstat = "";
        if (state == "E")
        {
            if (IsExists(txtCardNo.Text, hidID.Value.ToString()))
            {
                RegisterClientScriptBlock("abcd", "<script>alert('The cardno. already exists.')</script>");
                return;
            }
            sqlstat = "UPDATE [accounts] SET [cardNo] = '" + txtCardNo.Text + "',[userName] = '" + txtUserName.Text + "',[userPassword] = '" + txtPassword.Text + "',[balance] ='" + txtBalance.Text + "' WHERE id='" + hidID.Value.ToString() + "'";
        }
        else if (state == "N")
        {
            if (IsExists(txtCardNo.Text))
            {
                RegisterClientScriptBlock("abcd", "<script>alert('The cardno. already exists.')</script>");
                return;
            }
            sqlstat = "INSERT INTO [accounts]([cardNo],[userName],[userPassword],[balance]) VALUES('" + txtCardNo.Text + "','" + txtUserName.Text + "','" + txtPassword.Text + "','" + txtBalance.Text + "')";
        }
        if (CheckTextField())
        {
            da.RunSql(sqlstat);
            SetTextBoxValue("N", null, null, null, null, null);
            GridView1.DataBind();
            RegisterClientScriptBlock("abc", "<script>alert('Operation success.')</script>");
            SetOptableVisible(false);
        }
        else
        {
            RegisterClientScriptBlock("abcdef", "<script>alert('arguement error.')</script>");
            return;
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        SetTextBoxValue("N", null, null, null, null, null);
        SetOptableVisible(true);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        SetTextBoxValue("N", null, null, null, null, null);
        SetOptableVisible(false);
    }

    private void SetOptableVisible(bool b)
    {
        optable.Visible = b;
    }

    private bool IsExists(string cardNo)
    {
        DataAccess da = new DataAccess();
        SqlDataReader dr = da.GetDataReader("select cardNo from accounts where cardNo='" + cardNo + "'");
        if (dr.Read())
            return true;
        return false;
    }

    private bool IsExists(string cardNo, string oldId)
    {
        DataAccess da = new DataAccess();
        SqlDataReader dr = da.GetDataReader("select id from accounts where cardNo='" + cardNo + "'");
        if (dr.Read())
        {
            if (dr.GetValue(0).ToString() == oldId)
                return false;
            return true;
        }
        return false;
    }
    private bool CheckTextField()
    {
        if (txtCardNo.Text == "" || txtUserName.Text == "" || txtPassword.Text == "" || txtBalance.Text == "")
            return false;
        try
        {
            float.Parse(txtBalance.Text);
        }
        catch {
            return false;
        }
        return true;
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        GridView1.DataBind();
        GridView2.DataBind();
    }
}
